import string
import sys

def Parse (goldFile, silverFile, parametersFile, skipNum):
    goldHandle = file(goldFile, 'r')
    silverHandle = file(silverFile, 'r')
    results = []
    dates = []
    times = []
    rates = []
    goldPrices = []
    silverPrices = []
    
    print "Start reading files!"    
    for i in range(0, skipNum+1):
        goldStr = goldHandle.readline()
        silverStr = silverHandle.readline()
    
    goldStrList = goldStr.split(',')
    silverStrList = silverStr.split(',')
    
    tmpGoldPrice = 0.0
    tmpSilverPrice = 0.0
    while (len(goldStr)>0) and (len(silverStr)>0):
        if GetTimeKey(goldStrList)==GetTimeKey(silverStrList):
            dates.append(goldStrList[1])
            times.append(goldStrList[2])
            tmpGoldPrice = GetAvePrice(goldStrList)
            tmpSilverPrice = GetAvePrice(silverStrList)
            goldPrices.append(tmpGoldPrice)
            silverPrices.append(tmpSilverPrice)
            rates.append(tmpGoldPrice/tmpSilverPrice)
            goldStr = goldHandle.readline()
            silverStr = silverHandle.readline()
            goldStrList = goldStr.split(',')
            silverStrList = silverStr.split(',')
        elif GetTimeKey(goldStrList)<=GetTimeKey(silverStrList):
            goldStr = goldHandle.readline()
            goldStrList = goldStr.split(',')
        else:
            silverStr = silverHandle.readline()
            silverStrList = silverStr.split(',')            
    print "Finished reading files!"
            
    goldHandle.close()
    silverHandle.close()
    results.append(dates)
    results.append(times)
    results.append(rates)
    results.append(goldPrices)
    results.append(silverPrices)
    
    print "Start calculating moving average!"
    movingIntervals = GetParameters(parametersFile, 'Moving Average')
    for interval in movingIntervals:
        results.append(GetMovingAverage(rates, interval))        
    print "Finished calculating moving average!"
    
    print "Start calculating minmax!"
    minmaxIntervals = GetParameters(parametersFile, 'MinMax Interval')
    for interval in minmaxIntervals:
        minvalues, maxvalues = GetMinMax(rates, interval)
        results.append(minvalues)
        results.append(maxvalues)
    print "Finished calculating minmax!"
    return results

def GetMovingAverage(data, interval):
    result = [];
    for index in range(0, len(data)):
        result.append(0)
    intergral = 0.0
    if (interval > len(data)):
        return result
    else:
        intergral = sum(data[0:interval])
        result[interval-1] = intergral/interval
        for i in range(0, interval-1):
            result[i] = result[interval-1]
    for index in range(interval, len(data)):
        intergral -= data[index-interval]
        intergral += data[index]
        result[index] = intergral/interval
    return result

def GetMinMax(data, interval):
    print "interval ", interval
    resultmin = [];
    resultmax = [];
    for index in range(0, len(data)):
        resultmin.append(0)
        resultmax.append(0)
    minvalue, maxvalue, minindex, maxindex = GetMinMaxAndIndex(data, 0, interval)
    if (interval > len(data)):
        return (resultmin, resultmax)
    else:
        resultmin[interval-1] = minvalue
        resultmax[interval-1] = maxvalue
        for i in range(0, interval-1):
            resultmin[i] = resultmin[interval-1]
            resultmax[i] = resultmax[interval-1]
    recalculateNum = 0
    for index in range(interval, len(data)):
        if ((minindex <= (index-interval)) or (maxindex <= (index-interval))):
            recalculateNum = recalculateNum+1
            minvalue, maxvalue, minindex, maxindex = GetMinMaxAndIndex(data, (index-interval+1), interval)
        else:
            if (data[index]<minvalue):
                minvalue = data[index]
                minindex = index
            if (data[index]>maxvalue):
                maxvalue = data[index]
                maxindex = index
        resultmin[index] = minvalue
        resultmax[index] = maxvalue
    print "Re-calculate number: ", recalculateNum
    return (resultmin, resultmax)

def GetMinMaxAndIndex(data, begin, number):
    minvalue = data[begin]
    maxvalue = data[begin]
    minindex = begin
    maxindex = begin
    for index in range(begin, begin+number):
        if (data[index]<minvalue):
            minvalue = data[index]
            minindex = index
        if (data[index]>maxvalue):
            maxvalue = data[index]
            maxindex = index
    return (minvalue, maxvalue, minindex, maxindex)

def GetTimeKey(strList):
    return string.atol(strList[1]+strList[2])

def GetAvePrice(strList):
    return 0.5*(string.atof(strList[4]) + string.atof(strList[5]))
    
def GetParameters(fileName, key):
    parameterFile = file(fileName, 'r')
    parameters = []
    for line in parameterFile:
        if len(line)>0:
            strList = line.split('=')
            if (len(strList)==2) and (strList[0].strip()==key):
                movingIntervalStr = strList[1]
                for intervalStr in movingIntervalStr.split():
                    parameters.append(int(intervalStr))
    parameterFile.close()
    return parameters
    
if __name__ == "__main__":
    if (len(sys.argv) == 5):
        from time import clock
        print "Start calculating!"
        start = clock()
        results = Parse(sys.argv[1], sys.argv[2], sys.argv[3], int(sys.argv[4]))
        end = clock()
        print "Elapsed time: ", (end-start), " seconds."
        print "Finish calculating!\n"
        
        print "Start outputting data!"
        start = clock()
        outBuf = file('rates.dat', 'w')
        dataNum = len(results[0])
        for j in range(0, dataNum):
            for i in range(0, len(results)):
                outBuf.write(str(results[i][j]))
                outBuf.write('\t')
            outBuf.write('\n')
        outBuf.close()
        end = clock()
        print "Elapsed time: ", (end-start), " seconds."
        print "Finish outputting data!"
    else:
        print "Parameter is not acceptable!"
        
